<template>
  <div class="gallery-block" @mouseenter="isHover = true" @mouseleave="isHover = false">
    <div class="gallery-block-wrapper">
      <div
        class="gallery-block-row"
        :class="`gallery-block-row-cols-1 gallery-block-row-cols-md-${modelValue?.data?.length}`"
      >
        <div class="gallery-block-col" v-for="item in modelValue?.data" :key="item.id">
          <div
            class="gallery-block-image-block"
            @click="handleClick(item)"
          >
            <div
              class="gallery-block-image-bg"
              :class="{ 'gallery-block-image-bg-has-subtitle': item.subtitle }"
              :style="{ '--bg-image': `url('${item.imgSrc}')` }"
            ></div>
            <div class="gallery-block-image-content">
              <div class="gallery-block-image-border"></div>
              <div
                class="gallery-block-image-title"
                v-html="item.title"
              ></div>
              <div v-show="item.subtitle">
                <div
                  class="gallery-block-image-subtitle"
                  v-html="item.subtitle"
                ></div>
              </div>
              <div class="gallery-block-image-link">
                <div class="circle-link circle-link--light">
                  <!-- 如果链接包含 http 则使用 a 标签跳转 -->
                  <a v-if="item.url && item.url.includes('http')" :href="item.url" class="nuxt-link-exact-active nuxt-link-active">
                    <div>了解更多</div>
                    <span class="circle-link__icon circle-link__icon--light"></span>
                  </a>
                  <!-- 否则使用 button 并 emit handleClick 事件 -->
                  <button v-else @click="handleClick(item)" class="nuxt-link-exact-active nuxt-link-active">
                    <div>了解更多</div>
                    <span class="circle-link__icon circle-link__icon--light"></span>
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <Operate v-if="!isPreview" v-model:show="isHover" @handle-edit="showSetting = true" @handle-delete="handleDelete" @handle-copy="handleCopy" @handle-paste="handlePaste"></Operate>
  </div>

  <Setting v-if="!isPreview" v-model:show="showSetting" :data="modelValue"></Setting>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { randomString } from '@/utils/common'
import Operate from '@/common/Operate/index.vue'
import Setting from './setting.vue'

// 定义组件名称
defineOptions({
  name: 'GalleryBlock'
})

const props = defineProps({
  modelValue: {
    type: Object,
    default: () => ({})
  },
  isPreview: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['update:modelValue', 'handleDelete', 'handleCopy', 'handlePaste', 'handleClick'])

const handleInit = () => {
  if (props.modelValue && props.modelValue.data) return // 有数据不用初始化
  const data = {
    id: props.modelValue?.id || randomString(),
    type: 'GalleryBlock',
    data: [
      {
        id: randomString(),
        title: '标题',
        subtitle: '副标题',
        url: 'https://baidu.com',
        imgSrc: 'https://osswebsite.ycyw.com/media-library/ywies-bj/images/home/ywies-tx.jpg'
      },
      {
        id: randomString(),
        title: '标题',
        subtitle: '副标题',
        url: '/a/a',
        imgSrc: 'https://osswebsite.ycyw.com/media-library/ywies-bj/images/home/ywies-tx.jpg'
      }
    ],
  }
  emit('update:modelValue', data)
} 

const isHover = ref<boolean>(false);
const showSetting = ref<boolean>(false);

const handleDelete = () => {
  emit('handleDelete', props.modelValue.id)
}

const handleCopy = () => {
  emit('handleCopy', props.modelValue)
}

const handlePaste = (type: string) => {
  emit('handlePaste', type)
}

handleInit()

const handleClick = (item: any) => {
  if (item.url && item.url.includes('http')) {
    // 更安全的打开新窗口方式
    const newWindow = window.open(item.url, '_blank', 'noopener,noreferrer');
    if (newWindow) newWindow.opener = null;
    return
  }
  emit('handleClick', item)
}
</script>

<style lang="scss" scoped>
.gallery-block {
  position: relative;
}

.gallery-block-wrapper {
  max-width: 1920px;
  width: 100%;
  margin: 0 auto;

  @media (max-width: 575.98px) {
    width: 100vw;
  }
}

.gallery-block-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0;
}

.gallery-block-row-cols-1 {
  flex-basis: 100%;
}

.gallery-block-row-cols-md-2 .gallery-block-col {
  flex-basis: 50%;
}

.gallery-block-row-cols-md-3 .gallery-block-col {
  flex-basis: 33.333%;
}

.gallery-block-row-cols-md-4 .gallery-block-col {
  flex-basis: 25%;
}

.gallery-block-col {
  width: 100%;
}

.gallery-block-image-block {
  display: block;
  width: 100%;
  height: clamp(191px, calc((100vw - 320px) / 15.000 + 191px), 255px);
  position: relative;
  overflow: hidden;
  text-decoration: none;
  cursor: pointer;

  @media (max-width: 1279.98px) {
    height: clamp(255px, calc((100vw - 1280px) / 5.000 + 255px), 383px);
  }
}

.gallery-block-image-block:hover .gallery-block-image-bg {
  transform: scale(1);
}

.gallery-block-image-block:hover::after {
  opacity: 1;
}

.gallery-block-image-block:hover .gallery-block-image-content::before,
.gallery-block-image-block:hover .gallery-block-image-content::after {
  width: 100%;
}

.gallery-block-image-block:hover .gallery-block-image-border::before,
.gallery-block-image-block:hover .gallery-block-image-border::after {
  height: 100%;
}

.gallery-block-image-block:hover .gallery-block-image-link {
  max-height: 50px;
}

.gallery-block-image-block::after {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: #02246fb2;
  z-index: 1;
  opacity: 0;
  transition: opacity 0.3s ease-in-out;
}

.gallery-block-image-bg {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
  transition: transform 1s ease-in-out;
  transform: scale(1.1);
  background-image: linear-gradient(180deg, rgba(0, 0, 0, 0) 20.68%, rgba(0, 0, 0, 0.5) 90.52%), var(--bg-image);

  @media (max-width: 767.98px) {
    background-image: linear-gradient(180deg, rgba(0, 0, 0, 0) 23.83%, rgba(0, 0, 0, 0.51) 78.92%), var(--bg-image);
  }
}

.gallery-block-image-bg-has-subtitle {
  background-image: linear-gradient(180deg, rgba(0, 0, 0, 0) 20.68%, rgba(0, 0, 0, 0.1) 90.52%), linear-gradient(180deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.4) 100%), var(--bg-image) !important;

  @media (max-width: 767.98px) {
    background-image: linear-gradient(180deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.51) 73.03%), var(--bg-image) !important;
  }
}

.gallery-block-image-content,
.gallery-block-image-border {
  position: absolute;
}

.gallery-block-image-content::before,
.gallery-block-image-content::after,
.gallery-block-image-border::before,
.gallery-block-image-border::after {
  content: '';
  position: absolute;
  transition: all 0.3s ease-in-out;
}

.gallery-block-image-content::before,
.gallery-block-image-border::before {
  left: 0;
  top: 0;
}

.gallery-block-image-content::after,
.gallery-block-image-border::after {
  right: 0;
  bottom: 0;
}

.gallery-block-image-content {
  position: absolute;
  max-width: max(289px, 80.5%);
  z-index: 2;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 14px 19px;
}

.gallery-block-image-content::before,
.gallery-block-image-content::after {
  width: 0;
  height: 2px;
}

.gallery-block-image-content::before {
  background: linear-gradient(90deg, #ff4232 0%, #ff9e18 100%);
}

.gallery-block-image-content::after {
  background: linear-gradient(90deg, #ff0044 0%, #ff4232 100%);
}

.gallery-block-image-border {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}

.gallery-block-image-border::before,
.gallery-block-image-border::after {
  width: 2px;
  height: 0;
}

.gallery-block-image-border::before {
  background: linear-gradient(0, #ff0044 0%, #ff4232 100%);
}

.gallery-block-image-border::after {
  background: linear-gradient(0, #ff4232 0%, #ff9e18 100%);
}

.gallery-block-image-title {
  color: #fff;
  font-size: 28px;
  font-weight: 600;
  line-height: 1.58;
  text-align: center;
  white-space: nowrap;
  text-transform: uppercase;
}

.gallery-block-image-subtitle,
.gallery-block-image-subtitle ::v-deep * {
  position: relative;
  color: #fff;
  text-align: center;
  white-space: nowrap;
  font-size: 18px;
  line-height: 2.0559;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.gallery-block-image-subtitle {
  padding-top: 4px;
  margin-top: 4px;

  @media (max-width: 767.98px) {
    margin-top: 8px;
  }
}

.gallery-block-image-subtitle::before {
  content: '';
  position: absolute;
  left: 50%;
  top: 0;
  transform: translateX(-50%);
  width: 98px;
  border-top: 2px solid #fff;
  border-radius: 2px;
}

.gallery-block-image-link {
  position: relative;
  z-index: 3;
  margin-top: 5px;
  overflow: hidden;
  max-height: 0;
  display: flex;
  justify-content: center;
  transition: max-height 0.3s ease-in-out;
}

/* 让 button 看起来像链接 */
.circle-link button {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
  font: inherit;
  width: 100%;
  text-align: left;
}

/* 从 main.scss 迁移的 .link 样式 */
.link,
.link-body-p,
.link-caption,
.link-overline {
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  background-color: transparent;
  cursor: pointer;
  position: relative;
  text-decoration: none;
  color: #0048e8;
  color: var(--text-color, #0048e8);
}

.link-body-p:hover,
.link-caption:hover,
.link-overline:hover,
.link:hover {
  color: #0048e8;
  color: var(--text-color, #0048e8);
}

.link-overline {
  font-size: 14px;
  font-weight: 400;
  line-height: 1.285;
  letter-spacing: .06em;
  text-transform: uppercase;
  --text-color: #0048e8;
}

@media (max-width: 1023.98px) {
  .link-overline {
    font-size: 13px;
    line-height: 1;
  }
}

@media (max-width: 767.98px) {
  .link-overline {
    line-height: 1.166;
  }
}

.link-body-p {
  font-size: 16px;
  font-weight: 400;
  line-height: 1.75;
  --text-color: #0048e8;
}

@media (max-width: 1023.98px) {
  .link-body-p {
    font-size: 14px;
    line-height: 1.714;
    letter-spacing: .01em;
  }
}

.link-body-p.link--udrline:after {
  transform: scaleX(0) translateY(-.22em);
}

.link-body-p.link--udrline:hover:after {
  transform: scaleX(1) translateY(-.22em);
}

.link-caption {
  font-size: 14px;
  font-weight: 300;
  line-height: 1.214;
  --text-color: var(--text-color--primary);
}

@media (max-width: 1023.98px) {
  .link-caption {
    font-size: 13px;
    line-height: 1;
    letter-spacing: .04em;
  }
}

@media (max-width: 767.98px) {
  .link-caption {
    line-height: 1.33;
  }
}

.link--udrline {
  flex-direction: column;
}

.link--udrline--active:after,
.link--udrline:focus:after,
.link--udrline:hover:after {
  transform: scaleX(1);
}

.link--udrline:after {
  content: "";
  width: 100%;
  height: 1px;
  border-radius: 1px;
  background-color: #0048e8;
  background-color: var(--text-color, #0048e8);
  transform: scaleX(0);
  transition: all .3s ease-in-out;
}

.link--udrline-2 {
  display: inline-block;
}

.link--udrline-2 span {
  text-decoration: none;
  background-image: linear-gradient(90deg, #0048e8, #0048e8);
  background-image: linear-gradient(90deg, var(--text-color, #0048e8), var(--text-color, #0048e8));
  background-size: 0 1px, 100% 1px;
  background-position: 0 100%, 100% 100%;
  background-repeat: no-repeat;
  transition: background-size .3s;
  padding-bottom: 2px;
}

.link--udrline-2--active span,
.link--udrline-2:focus span,
.link--udrline-2:hover span {
  background-size: 100% 1px, 0 1px;
}

.link-list__item:not(:first-child) {
  margin-left: 40px;
}

@media (max-width: 767.98px) {
  .link-list__item:not(:first-child) {
    margin-left: 24px;
  }
}

.link-list.center .link-list__container {
  justify-content: center;
}

.text-link {
  display: flex;
}

.text-link__wrapper {
  background: none;
  padding: 0;
  border: none;
  --text-color: #0032a0;
}

.text-link--light .text-link__wrapper {
  --text-color: #fff;
}

.circle-link {
  display: flex;
  --link-color: var(--link-blue-color);
}

.circle-link--light {
  --link-color: var(--link-light-color);
}

.circle-link a,
.circle-link button {
  font-size: 14px;
  font-weight: 500;
  line-height: 1.285;
  letter-spacing: .04em;
  border: none;
  background: none;
  color: var(--link-color);
  text-decoration: none;
  display: flex;
  align-items: center;
  padding: 0;
}

@media (max-width: 1023.98px) {
  .circle-link a,
  .circle-link button {
    line-height: 1.428;
  }
}

@media (max-width: 767.98px) {
  .circle-link a,
  .circle-link button {
    line-height: 1.285;
  }
}

.circle-link a:hover .circle-link__icon:before,
.circle-link button:hover .circle-link__icon:before {
  transform: translate(-50%, -50%);
}

.circle-link a:hover .circle-link__icon:after,
.circle-link button:hover .circle-link__icon:after {
  transform: translate(150%, -50%);
}

.circle-link a:active .circle-link__icon:before,
.circle-link button:active .circle-link__icon:before {
  transform: translate(-260%, -50%);
}

.circle-link a:active .circle-link__icon:after,
.circle-link button:active .circle-link__icon:after {
  transform: translate(-50%, -50%);
}

.circle-link__icon {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  overflow: hidden;
  background-color: var(--link-color);
  position: relative;
  margin-left: 7px;
  flex-shrink: 0;
}

@media (max-width: 767.98px) {
  .circle-link__icon {
    width: 12px;
    height: 12px;
    margin-left: 4px;
  }
}

.circle-link__icon:after,
.circle-link__icon:before {
  content: "";
  width: 6px;
  height: 9px;
  position: absolute;
  top: 50%;
  left: 55%;
  background-image: url();
  background-position: 50%;
  background-size: cover;
  background-repeat: no-repeat;
  background-color: hsla(0,0%,100%,0);
  transition: transform .3s ease-in-out;
}

@media (max-width: 767.98px) {
  .circle-link__icon:after,
  .circle-link__icon:before {
    width: 4.7px;
    height: 6px;
    background-image: url();
    background-position: 50%;
    background-size: cover;
    background-repeat: no-repeat;
    background-color: hsla(0,0%,100%,0);
  }
}

.circle-link__icon:before {
  transform: translate(-260%,-50%);
}

.circle-link__icon:after {
  transform: translate(-50%,-50%);
}

.circle-link__icon--light:after,.circle-link__icon--light:before {
  background-image: url();
  background-position: 50%;
  background-size: cover;
  background-repeat: no-repeat;
  background-color: hsla(0,0%,100%,0);
}

@media (max-width: 767.98px) {
  .circle-link__icon--light:after,.circle-link__icon--light:before {
    background-image:url();
    background-position: 50%;
    background-size: cover;
    background-repeat: no-repeat;
    background-color: hsla(0,0%,100%,0);
  }
}
</style>